--[[
    房间实体
]]--

local blaze = require "blaze"
local game = require "club.threecard.blackjack"
local Room = require "entities.Room"
local log = blaze.logging.get("BlackjackRoom")

local M = game.kbe.entity("BlackjackRoom", Room)

function M:__init__()
    Room.__init__(self)
end
local phases = {
    [0] = "空闲",
    [1] = "准备",
    [2] = "发牌",
    [3] = "保险",
    [4] = "保险结果",
    [5] = "玩家",
    [6] = "庄家",
    [7] = "结算"
}

--[[ =============================
             服务端接口
================================== ]]--

--[[ =============================
             服务端回调
================================== ]]--

function M:set_bet_amounts(old)
    game.store.ingame.on_bet_amounts(self.bet_amounts);
    log.debug("set_bet_amounts")
end

function M:on_phase(phase_id, countdown)
    game.store.ingame.on_phase({phase_id = phase_id, countdown = countdown});

    local phase_name = phases[phase_id] or "未知"
    if countdown > 0 then
        log.debug(string.format("进入房间阶段：%s 倒计时%d毫秒", phase_name, countdown))
    else
        log.debug(string.format("进入房间阶段：%s", phase_name))
    end
end

function M:on_reveal(card_id)
    log.debug(string.format("庄家展示底牌：%d", card_id))
    game.store.ingame.on_banker_reveal(card_id);
end

function M:on_insure_result(blackjack, claim_info)
    log.debug(string.format("保险结果： blackjack:%s claim_info:%s", blackjack,  blaze.dump(claim_info)));
    game.store.ingame.on_insure_result({blackjack = blackjack, claim_info = claim_info});
    
    if blackjack == 1 then
        log.debug(string.format("保险结果：庄家21点，等待庄家展示底牌"))
    else
        log.debug(string.format("保险结果：庄家不是21点，本局继续"))
    end
end

function M:on_deal(card_ids)
    log.debug(string.format("庄家摸牌：%s", blaze.dump(card_ids)))
    game.store.ingame.on_banker_deal(card_ids)
end

-- {{no, payout}}
function M:on_game_result(result_info)
    log.debug(string.format("结算%s", blaze.dump(result_info)))
    game.store.ingame.on_game_result(result_info);
end

-- @scene{banker: card_ids, players: {no = 0, card_ids}}
function M:on_game_start(scene)
    log.debug("游戏开始:"..blaze.dump(scene));
    game.store.ingame.on_game_start(scene)
end

return M